/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.veles;

import com.google.common.base.Preconditions;
import cz.muni.fi.veles.maps.AbstractMap;
import javax.faces.component.UIComponent;

/**
 * Class provides static method, which are used across component library
 * @author Jan Ferko
 * @version 1.0
 */
public class MapUtils {
    
    /**
     * Method return map server of parent map component. 
     * 
     * @see {@link MapServer}
     * @param component component, which map server should be resolved. Should not be null.
     * @return map server of parent map
     */
    public static MapServer resolveMapServer(UIComponent component){
        Preconditions.checkNotNull(component);
        final AbstractMap map = MapUtils.getParentMap(component);
        return map.getMapServer();
    }
    
    /**
     * Method return parent map of component.
     * @param component component, which parent map is wanted. Should not be {@code null}
     * @return  parent map or null if component does not have map ancestor
     */
    public static AbstractMap getParentMap(UIComponent component){
        Preconditions.checkNotNull(component);
        UIComponent parent = component.getParent();
        while(parent != null && !(parent instanceof AbstractMap)){
            parent = parent.getParent();
        }
        AbstractMap map = (AbstractMap) parent;
        return map;
    }
    
}
